package model;

import java.awt.Toolkit;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.Icon;
import javax.swing.ImageIcon;

/**
 *
 * @author Joachim
 */
public class GameField {
    GameTile [][] field;
    AnimatedTile player;
    List<AnimatedTile> enemies;
    
    public GameField(File level) {
        field = new GameTile [Settings.GAME_HEIGHT_IN_TILES][Settings.GAME_WIDTH_IN_TILES];
        enemies = new ArrayList<AnimatedTile>();
        Icon wall = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/resources/Muur.jpg"))); 
        Icon floor = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/resources/Vloer.png"))); 
        
        FileReader fr = null;
        try {
            fr = new FileReader(level);
            BufferedReader br = new BufferedReader(fr);
            String line = null;
            for (int i = 0; i < field.length; i++) {
                line = br.readLine();
                String [] tiles = line.split(" ");
                if(tiles.length==field[i].length) {
                    for (int j=0; j<tiles.length; j++) {
                        String tile = tiles [j];
                        if(tile.equals("W")) {
                            field[i][j] = new GameTile(i, j, wall);
                        } else if(tile.equals("F")) {
                            field[i][j] = new GameTile(i, j, floor);
                        } else {
                            System.err.println("Incorrect level file");
                            System.exit(1);
                        }
                    }
                } else {
                    System.err.println("Incorrect level file");
                    System.exit(1);
                }
            }
            line = br.readLine();
            while(line != null) {
                String [] object = line.split(" ");
                if(object.length==3) {
                    if(object[0].equals("Player:")) {
                        player = new AnimatedTile(Integer.parseInt(object[1]), Integer.parseInt(object[2]), true, null);
                    } else if(object[1].equals("Enemy:")) {
                        enemies.add(new AnimatedTile(Integer.parseInt(object[1]), Integer.parseInt(object[2]), false, null));
                    }
                }
                line = br.readLine();
            }
            
        } catch (IOException ex) {
          if(ex instanceof FileNotFoundException) {
              System.err.println("File not found");
          } else {
              System.err.println("IOException while reading line from file");
          }
        } finally {
            try {
                fr.close();
            } catch (IOException ex) {
                System.err.println("IOException while closing file");
            }
        }
    }

    public GameTile[][] getField() {
        return field;
    }
}
